package com.example.project1;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

import com.example.project1.help.Mesh;
import com.scoreninja.adapter.ScoreNinjaAdapter;
import com.swarmconnect.Swarm;
import com.swarmconnect.SwarmActivity;



/**
 * 
 * @author Ballach
 * implements activity lifecycle
 * setups the applications
 * 
 * Droid Invaders is a Space Invaders clone for android. The HighScore uses the Swarm framework to provide a global leaderboard.
 * Swarm provides connectivity to twitter/facebook and let you create a swarm account to manage games wich uses the swarm framework.
 * this project use opengl 
 *
 */
public class GameActivity extends SwarmActivity implements
		GLSurfaceView.Renderer, OnTouchListener, SensorEventListener {

	private GLSurfaceView glSurface;
	private int width, height;
	private GameListener listener;
	long lastFrameStart;
	float deltaTime;
	int touchX, touchY;
	boolean isTouched;
	int scoreOver;

	float[] acceleration = new float[3];

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		/**
		 * uses the swarm framework to add social components and a global leaderboard
		 * uses(context, appid, privatekey
		 * is free to use/internetconnection nessacery
		 */
		Swarm.init(this, 3132, "f35e0ae0b08329c6e6bb77595a28ac6c");

		glSurface = new GLSurfaceView(this);
		glSurface.setRenderer(this);
		this.setContentView(glSurface);

		glSurface.setOnTouchListener(this);

		/**
		 * setup sensor
		 */
		SensorManager manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		if (manager.getSensorList(Sensor.TYPE_ACCELEROMETER).size() > 0) {
			Sensor accelerometer = manager.getSensorList(
					Sensor.TYPE_ACCELEROMETER).get(0);
			manager.registerListener(this, accelerometer,
					SensorManager.SENSOR_DELAY_GAME);
		}
	}


	@Override
	public void onDrawFrame(GL10 gl) {
		long currentFrameStart = System.nanoTime();
		deltaTime = (currentFrameStart - lastFrameStart) / 1000000000.0f;
		lastFrameStart = currentFrameStart;

		if (listener != null)
			listener.mainLoopIteration(this, gl);
	}

	@Override
	public void onSurfaceChanged(GL10 gl, int width, int height) {
		this.width = width;
		this.height = height;
	}

	/**
	 * glsurface finished initialization
	 */
	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		lastFrameStart = System.nanoTime();
		String renderer = gl.glGetString(GL10.GL_RENDERER);
		if (renderer.toLowerCase().contains("pixelflinger"))
			Mesh.globalVBO = false;

		if (listener != null)
			listener.setup(this, gl);
	}

	@Override
	protected void onPause() {
		super.onPause();
		glSurface.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();
		glSurface.onResume();
	}

	/**
	 * touch events occurs
	 */
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN
				|| event.getAction() == MotionEvent.ACTION_MOVE) {
			touchX = (int) event.getX();
			touchY = (int) event.getY();
			isTouched = true;
		}

		if (event.getAction() == MotionEvent.ACTION_UP)
			isTouched = false;

		try {
			Thread.sleep(30);
		} catch (Exception ex) {

		}

		return true;
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		System.arraycopy(event.values, 0, acceleration, 0, 3);
	}

	public void setGameListener(GameListener listener) {
		this.listener = listener;
	}

	public int getViewportWidth() {
		return width;
	}

	public int getViewportHeight() {
		return height;
	}

	public float getDeltaTime() {
		return deltaTime;
	}

	public int getTouchX() {
		return touchX;
	}

	public int getTouchY() {
		return touchY;
	}

	public boolean isTouched() {
		return isTouched;
	}

	public float getAccelerationOnXAxis() {
		return acceleration[0];
	}

	public float getAccelerationOnYAxis() {
		return acceleration[1];
	}

	public float getAccelerationOnZAxis() {
		return acceleration[2];
	}

}
